# collections 中 Counter,defaultdic,orderedDict 的使用


# 1.Counter统计个数

from collections import Counter, defaultdict, OrderedDict, deque

# 统计元素出现次数
words = ["apple", "banana", "apple", "orange", "banana", "apple"]
word_count = Counter(words)
print("词频统计:", word_count)
print("出现最多的2个:", word_count.most_common(2))

# 更新计数器
word_count.update(["apple", "grape"])
print("更新后的计数器:", word_count)


# 默认字典
dd = defaultdict(list)
dd["fruits"].append("apple")
dd["fruits"].append("banana")
dd["vegetables"].append("carrot")
print("默认字典:", dict(dd))


# 有序字典
ordered_dict = OrderedDict()
ordered_dict["z"] = 1
ordered_dict["a"] = 2
ordered_dict["b"] = 3
ordered_dict["d"] = 4
print(list(ordered_dict.keys()))

# deque双端队列
nums_deque = deque([4, 5, 6])

# 左边添加
for i in range(3, 0, -1):
    nums_deque.appendleft(i)

# 往队列的右边添加
for i in range(7, 10):
    nums_deque.append(i)


print("type(nums_deque)=", type(nums_deque), "nums_deque=", nums_deque)
